#include "markdown_editer.h"

markdown_editer::markdown_editer(QString FilePath, QWidget *parent):
    doc(nullptr)
    ,pre(nullptr)
    ,pre_area(nullptr)
    ,EditBaseWidget(FilePath, parent)
{

}

void markdown_editer::init_extra_member_val()
{
    pre_area = new QWebEngineView(this);
    doc = new Document(text_edit);
    pre = new PreviewPage(pre_area);
    pre_area->setPage(pre);
    QWebChannel* channel = new QWebChannel(dynamic_cast<QWidget*>(this) );
    channel->registerObject(QStringLiteral("content"),doc);
    pre->setWebChannel(channel);
    pre->setUrl(QUrl("qrc:/markdown/index.html"));

}

void markdown_editer::resize_editer(int w, int h)
{
    this->resize(w, h);
    if (text_edit){
        text_edit->resize(w,h);
    }
        int mid_point = (int) w/2;
        text_edit->resize(mid_point, h);
        pre_area->resize(mid_point, h);
        text_edit->move(0,0);
        pre_area->move(mid_point,0);
}



void markdown_editer::InitUiStyle()
{
    text_edit->setPlainText(file_content);
    resize_editer(width(), height());
    file_edited();
    connect(text_edit, SIGNAL(textChanged()), this, SLOT(file_edited()));
}

void markdown_editer::file_edited()
{
    file_new_content = text_edit->toPlainText();
    doc->setText(file_new_content);
}
